<?php

/**
 * JingYao-backend
 *
 * @link     https://gitee.com/wang-zhihui-release/jingyao-backend
 * @apiDocument https://gitee.com/wang-zhihui-release/jingyao-backend/wikis/
 */

namespace App\Http\Controllers;

use App\Exceptions\ApiException;
use App\Format\ProposalFormat;
use App\Http\Requests\CreateProposalRequest;
use App\Services\ProposalService;
use Illuminate\Http\Request;

class ProposalController extends Controller
{
    protected $proposalService;

    public function __construct(ProposalService $proposalService)
    {
        $this->proposalService = $proposalService;
    }

    public function createProposal(CreateProposalRequest $createProposalRequest)
    {
        $format = new ProposalFormat($createProposalRequest->all());
        $this->proposalService->createProposal($format);
        return renderSuccessJson();
    }

    public function getOwnProposalList(Request $request)
    {
        $userId = $request->input('user_id');
        $search = [];
        if ($request->input('start_date')) {
            $search['start_date'] = $request->input('start_date');
        }
        if ($request->input('end_date')) {
            $search['end_date'] = $request->input('end_date');
        }
        if ($request->input('title')) {
            $search['title'] = $request->input('title');
        }
        if ($request->input('order_column')) {
            $search['order_column'] = $request->input('order_column');
        }
        if ($request->input('order_type')) {
            $search['order_type'] = $request->input('order_type', 'desc');
        }
        $page = $request->input('page', 1);
        $pageSize = $request->input('page_size', 100);
        $proposals = $this->proposalService->getUserProposals($page, $pageSize, $userId, $search);
        return renderSuccessJson($proposals);
    }

    public function getProposalDetail(Request $request)
    {
        $userId = $request->input('user_id');
        $id = $request->input('id');
        if (empty($id)) {
            throw new ApiException('缺少参数');
        }
        $detail = $this->proposalService->getUserProposalDetail($id, $userId);
        return renderSuccessJson($detail);
    }

    public function setScore(Request $request)
    {
        $id = $request->input('id');
        $score = $request->input('score');
        $remarks = $request->input('remarks', '');
        $this->proposalService->setScore($id, $score, $remarks);
        return renderSuccessJson();
    }

    public function getList(Request $request)
    {
        $page = $request->input('page', 1);
        $pageSize = $request->input('page_size', 10);
        $search = [];

        if ($request->input('title')) {
            $search['title'] = $request->input('title');
        }

        if ($request->input('reply')) {
            $search['reply'] = $request->input('reply');
        }

        $res = $this->proposalService->getlists($page, $pageSize, $search);

        return renderSuccessJson($res);
    }

    public function reMessage(Request $request)
    {
        $id = $request->input('id');
        $reMessage = $request->input('re_message');
        $answerUserId = $request->input('user_id');
        $re_images = empty($request->input('re_images')) ? '' : $request->input('re_images');
        if (empty($reMessage) || empty($id) || empty($answerUserId)) {
            throw new ApiException('缺少参数', 1);
        }
        $this->proposalService->reMessage($id, $answerUserId, $re_images, $reMessage);
        return renderSuccessJson();
    }

    public function deleteProposal(Request $request)
    {
        $id = $request->input('id');
        $this->proposalService->delete($id);
        return renderSuccessJson();
    }

    public function adminGetProposalDetails(Request $request)
    {
        $id = $request->input('id');
        $res = $this->proposalService->adminGetProposalDetails($id);
        return renderSuccessJson($res);
    }
}
